﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieRental.BuisnessComponents.Helpers;
using MovieRental_Presentation.Helpers;
using MovieRental.Entities;
using System.Collections.ObjectModel;
using MovieRental.Charts;
using System.Windows.Controls;

namespace MovieRental_Presentation.DataViews
{
    public class StatisticsDataView : BaseDataView
    {
        private double _spentMoney;
        private int _quantity;
        private DateTime _startDate = DateTime.Today;
        private DateTime _endDate = DateTime.Today;


        private ObservableCollection<PersonalDiscountInfo> _personalDiscounts = new ObservableCollection<PersonalDiscountInfo>();
        private ObservableCollection<CartDiscountInfo> _cartDiscounts = new ObservableCollection<CartDiscountInfo>();

        private CartDiscountInfo _cartDiscount;
        private PersonalDiscountInfo _personalDiscount;


        public StatisticsDataView(Canvas chartCanvas)
        {
            ComponentManager.Instance.CartDiscountComponent.SourceUpdated += MakeEventHandlerForProperty("CartDiscounts");
            ComponentManager.Instance.PersonalDiscountComponent.SourceUpdated += MakeEventHandlerForProperty("PersonalDiscounts");
            DateTime t = DateTime.Today;
            Chart = new TimeMoneyChart();
            Chart.ChartCanvas = chartCanvas;
            StartDate = new DateTime(t.Year, t.Month, 1);
            EndDate = new DateTime(t.Year, t.Month, DateTime.DaysInMonth(t.Year, t.Month));
        }

        public TimeMoneyChart Chart { get; set; }

        public void UpdateIncomeForPeriod()
        {
            var query = ComponentManager.Instance.LoanCartComponent.GetIncomeForPeriod(StartDate, EndDate);
            Chart.DataSeries = new TimeDataSeries<double>();
            TimeDataSeries<double> series = new TimeDataSeries<double>();
            foreach (var item in query)
                series.AddPoint(item.Key, item.Value);
            Chart.DataSeries = series;
        }

        public DateTime StartDate
        {
            get { return _startDate; }
            set
            {
                SetProperty<DateTime>(ref _startDate, value, "StartDate");
                if (_startDate > EndDate)
                    EndDate = _startDate.AddDays(1);
                UpdateIncomeForPeriod();
            }
        }

        public DateTime EndDate
        {
            get { return _endDate; }
            set
            {
                SetProperty<DateTime>(ref _endDate, value, "EndDate");
                if (_endDate < StartDate)
                    StartDate = _endDate.AddDays(-1);
                UpdateIncomeForPeriod();
            }
        }

        public double SpentMoney
        {
            set
            {
                SetProperty<double>(ref _spentMoney, value, "SpentMoney");
                OnPropertyChanged("PersonalDiscountBound");
            }
            get { return _spentMoney; }
        }

        public int Quantity
        {
            set
            {
                SetProperty<int>(ref _quantity, value, "Quantity");
                OnPropertyChanged("CartDiscountBound");
            }
            get { return _quantity; }
        }

        public ValueBound<double> CartDiscountBound
        {
            get 
            { 
                var bound = ComponentManager.Instance.CartDiscountComponent.GetValueBoundsForCondition(Quantity);
                return new ValueBound<double>(bound.Lower + 0.5, bound.Upper - 0.5);
            }
        }

        public ValueBound<double> PersonalDiscountBound
        {
            get
            {
                var bound = ComponentManager.Instance.PersonalDiscountComponent.GetValueBoundsForCondition(SpentMoney);
                return new ValueBound<double>(bound.Lower + 0.5, bound.Upper - 0.5);
            }
        }

        public ObservableCollection<PersonalDiscountInfo> PersonalDiscounts
        {
            get
            {
                UpdateObservableCollection<PersonalDiscountInfo>(_personalDiscounts, ComponentManager.Instance.PersonalDiscountComponent.GetAll());
                return _personalDiscounts;
            }
        }

        public ObservableCollection<CartDiscountInfo> CartDiscounts
        {
            get
            {
                UpdateObservableCollection<CartDiscountInfo>(_cartDiscounts, ComponentManager.Instance.CartDiscountComponent.GetAll());
                return _cartDiscounts;
            }
        }

        public PersonalDiscountInfo PersonalDiscount
        {
            set { SetProperty<PersonalDiscountInfo>(ref _personalDiscount, value, "PersonalDiscount"); }
            get { return _personalDiscount; }
        }

        public CartDiscountInfo CartDiscount
        {
            set { SetProperty<CartDiscountInfo>(ref _cartDiscount, value, "CartDiscount"); }
            get { return _cartDiscount; }
        }
    }
}
